<cfcomponent output="false" hint="PDF reader." extensions="pdf" extends="reader">

<cffunction name="read" access="public" returnType="string" output="false">
	<cfargument name="file" type="string" required="true">

	<cfset var results = arrayNew(1)>
	<cfset var ddx = "">
	<cfset var inputStruct = "">
	<cfset var outputStruct = "">
	<cfset var tempxml = getTempFile(getTempDirectory(), "pdfutils")>
	<cfset var ddxVar = "">
	<cfset var myxml = "">
	<cfset var x = "">
	<cfset var node = "">
	<cfset var text = "">

	<!--- does the file exist? --->
	<cfif not fileExists(arguments.file)>
		<cfthrow message="Unable to find pdf: #arguments.file#">
	</cfif>
	
	<!--- Create DDX --->
	<cfsavecontent variable="ddx">
<?xml version="1.0" encoding="UTF-8"?>
<DDX xmlns="http://ns.adobe.com/DDX/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd">
<DocumentText result="Out1">
<PDF source="doc1"/>
</DocumentText>
</DDX>
	</cfsavecontent>
	
	<cfset ddx = trim(ddx)>
	
	<!--- Set my parameters --->
	<cfset inputStruct = {doc1="#arguments.file#"}>
	<cfset outputStruct = {Out1="#tempxml#"}>
	
	<!--- Process --->
	<cfpdf action="processddx" ddxfile="#ddx#" inputfiles="#inputStruct#" outputfiles="#outputStruct#" name="ddxVar">
	
	<!--- read in and parse xml --->
	<cffile action="read" file="#tempxml#" variable="myxml">
	<cfset myxml = xmlParse(myxml)>
	
	<!--- ensure valid stuff --->
	<cfif structKeyExists(myxml, "DocText") and structKeyExists(myxml.DocText,"TextPerPage") and structKeyExists(myxml.DocText.TextPerPage, "Page")>
		
		<cfloop index="x" from="1" to="#arrayLen(myxml.DocText.TextPerPage.Page)#">
			<cfset node = myxml.DocText.TextPerPage.Page[x]>
			<cfset text = node.xmltext>
			<cfset arrayAppend(results, text)>
		</cfloop>
		
	</cfif>

	<cfreturn arraytolist(results)>	
</cffunction>

</cfcomponent>